package com.mdd.front.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.SystemMsg;
import com.mdd.common.util.StringUtils;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.IMsgService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.vo.msg.SystemMsgVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/msg")
@Api(tags = "系统消息相关API")
public class MsgController {

	@Autowired
	private IMsgService iMsgService;
	
	
	/**
	 * 	获取消息列表
	 * @param pageValidate
	 * @param type		1=快点消息 2=快商消息 3=平台公告
	 * @return
	 */
	@GetMapping("/qryMsgList")
	@ApiOperation(value = "获取消息列表")
	public AjaxResult<PageResult<SystemMsg>> qryMsgList(@Validated PageValidate pageValidate,Integer type) {
		Integer userId = LikeFrontThreadLocal.getUserId();
		if(null == type) type = 1;
		return AjaxResult.success(iMsgService.qryMsgList(userId, type, pageValidate)); 
	}
	
	@GetMapping("/qryMsgFront")
	@ApiOperation(value = "获取消息前一条")
	public AjaxResult<List<SystemMsgVo>> qryMsgFront() {
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iMsgService.qryMsgFront(userId); 
	}
	
	
	@PostMapping("/cleanUnreadMsg")
	@ApiOperation(value = "清除未读消息")
	public AjaxResult<Object> cleanUnreadMsg(@RequestBody SystemMsg msg) {
		Integer userId = LikeFrontThreadLocal.getUserId();
		if(null == msg || null == msg.getType()) {
			return AjaxResult.failed("请选择消息");
		}
		return iMsgService.cleanUnreadMsg(userId,msg.getType()); 
	}
	
	@NotLogin
	@PostMapping("/sendExpressDeliveryMsg")
	@ApiOperation(value = "发送快递消息")
	public AjaxResult<Object> sendExpressDeliveryMsg(@RequestBody SystemMsg msg) {
		if(null == msg) return AjaxResult.failed("请选择用户");
		if(null == msg.getUserId()) return AjaxResult.failed("请选择用户");
		if(StringUtils.isEmpty(msg.getTitle())) return AjaxResult.failed("请输入标题");
		if(StringUtils.isEmpty(msg.getContent())) return AjaxResult.failed("请输入内容");
		return AjaxResult.success(iMsgService.sendExpressDeliveryMsg(msg)); 
	}
	
}
